package poidemo.docx4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.jaxb.NamespacePrefixMappings;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class DocToHtml {

	public static void main(String[] args)
			throws IOException, TransformerException, ParserConfigurationException, Docx4JException, NoSuchFieldException, IllegalAccessException {
		test2(args);
	}

	public static void test2(String[] args)
			throws Docx4JException, FileNotFoundException, NoSuchFieldException, IllegalAccessException {

		WordprocessingMLPackage docxPkg = WordprocessingMLPackage
				.load(new File("D:\\tmp\\docx\\test61 - 副本.docx"));

		HTMLSettings settings = Docx4J.createHTMLSettings();
		settings.setCustomXsltTemplates(loadCustomTemplates());
		settings.setOpcPackage(docxPkg);
		settings.setImageDirPath("D:\\tmp\\docx\\TEST\\test6");
		settings.setImageTargetUri("test6");
		Docx4J.toHTML(settings, new FileOutputStream("D:\\tmp\\docx\\test61.html"),
				0);
	}

	protected static Templates loadCustomTemplates() throws Docx4JException {
		Source xsltSource = null;
		Templates ret = null;
		URIResolver originalURIResolver = null;
		try {
			originalURIResolver = XmlUtils.getTransformerFactory().getURIResolver();
			XmlUtils.getTransformerFactory().setURIResolver(new OutHtmlURIResolver());
			if (Docx4jProperties.getProperty(PROPERTY_HTML_OUTPUT_TYPE, true)) {
				defaultTemplatesResource = XHTML_TEMPLATE_RESOURCE;
			} else {
				defaultTemplatesResource = HTML_TEMPLATE_RESOURCE;
			}
			xsltSource = new StreamSource(
					DocToHtml.class.getResourceAsStream(defaultTemplatesResource));
			ret = XmlUtils.getTransformerTemplate(xsltSource);
		} catch (TransformerConfigurationException e) {
			throw new Docx4JException(
					"Exception loading template \"" + defaultTemplatesResource + "\", "
							+ e.getMessage(), e);
		} finally {
			XmlUtils.getTransformerFactory().setURIResolver(originalURIResolver);
		}
		return ret;
	}

	protected static String defaultTemplatesResource = null;

	protected static final String PROPERTY_HTML_OUTPUT_TYPE =
			"docx4j.Convert.Out.HTML.OutputMethodXML";

	protected static final String XHTML_TEMPLATE_RESOURCE =
			"/templates/docx2xhtml.xslt";
	protected static final String HTML_TEMPLATE_RESOURCE =
			"/templates/docx2html.xslt";

	protected static final String XSLT_RESOURCE_ROOT =
			"/templates/";

	protected static class OutHtmlURIResolver implements URIResolver {

		@Override
		public Source resolve(String href, String base) {
			return new StreamSource(
					DocToHtml.class.getResourceAsStream(XSLT_RESOURCE_ROOT + href));
		}
	}
}
